(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.siteDate.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$q', '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'siteService' ];
	function updateVerifyController($q, $scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, siteService) {
		var ctrl = this, siteInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.authService = authService;
		ctrl.site = {};
		
		ctrl.init = function() {
			authService.updateVerify(siteInfo).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.site = response.data.model.newModel;
					ctrl.siteOld = response.data.model.oldModel;
					ctrl.site.taskId = response.data.task.taskId;
				}
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.site.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.site).then(approveSuccessful, submitFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.site.comments = comments;
						authService.updateVerifyReject(ctrl.site).then(rejectSuccessful, submitFailed);
					}
				}
			});
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.site.list',
					'state' : 'cda.site.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.site.list',
					'state' : 'cda.site.list',
					'params' : {}
				}]
			});
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
